package com.diyunapp.xilinapp.myindent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.ParamsBean;
import com.diyunapp.xilinapp.bean.TuihuoBean;
import com.diyunapp.xilinapp.details.GoodsDetailActivity;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefundSalesActivity extends StatusBarFragmentActivity {
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.tuihuo_list)
    ListView tuihuoList;
    @Bind(R.id.tuihuo_refresh_view)
    SwipeRefreshLayout tuihuoRefreshView;
    private List<TuihuoBean.DataBean> info = new ArrayList<>();
    private LayoutInflater inflater;
    private TuiHuoAdapter tuiHuoAdapter;

    //退款退货
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_sales);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.refundsale_menubar_inc);
        super.MenuWidget(headView);
        tuiHuoAdapter=new TuiHuoAdapter(this);
        initfootmarks();
//        getData("http://www.xilingou.com/mobile/index.php?act=goods&op=refund_list&member_id=70");
        tuihuoList.setAdapter(tuiHuoAdapter);

        tuihuoRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        tuihuoRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                initfootmarks();
            }
        });



    }

    private void initfootmarks() {
        String url = ""+MainActivity.url+"act=goods&op=refund_list&member_id="
                + MainActivity.myLogin.getMemberid()+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        tuihuoRefreshView.setRefreshing(false);
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            if (result == 200) {
                                info.clear();
                                JSONArray listArray = resultObject.optJSONArray("data");
                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.optJSONObject(i);
                                    String refund_id=recordObject.optString("refund_id");
                                    String store_name = recordObject.optString("store_name");
                                    String goods_name = recordObject.optString("goods_name");
                                    String goods_num = recordObject.optString("goods_num");
                                    String refund_type = recordObject.optString("refund_type");
                                    String seller_state = recordObject.optString("seller_state");
                                    String return_type = recordObject.optString("return_type");
                                    String goods_image=recordObject.optString("goods_image");
                                    String goods_id=recordObject.optString("goods_id");
                                    String goods_params = recordObject.optString("goods_spec");
                                    StringBuffer paramsBuffer = new StringBuffer("");
                                    if (goods_params == null || goods_params.isEmpty() || goods_params.equals("null")) {
                                    } else {
                                        List<ParamsBean> goodsParamsList = FormatConfirm.getParameters(goods_params);

                                        for (int j = 0; j < goodsParamsList.size(); j++) {
                                            paramsBuffer.append(goodsParamsList.get(j).getParamsKey());
                                            paramsBuffer.append("  ");
                                            Log.i("zhangxiaoxidebug", "resultinfo11=" + paramsBuffer.toString());
                                        }
                                    }


                                    info.add(new TuihuoBean.DataBean(refund_id,store_name, goods_name, goods_num, refund_type,
                                            seller_state, return_type,goods_image,goods_id,paramsBuffer.toString()));
                                    Log.i("zhangxiaoxidebug", "resultinfo11=" + info);
//                                    Log.i("===============", "resultinfo=" + info);
                                }

                                tuiHuoAdapter.setList(info);

                                tuiHuoAdapter.notifyDataSetChanged();

                            } else {
                                toast("当前暂无数据");
                                tuihuoRefreshView.setVisibility(View.GONE);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            tuihuoRefreshView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        tuihuoRefreshView.setRefreshing(false);
                        toast("请求失败");
                        tuihuoRefreshView.setVisibility(View.GONE);
                    }
                });
    }

    class TuiHuoAdapter extends BaseAdapter {
        private List<TuihuoBean.DataBean> info = new ArrayList<TuihuoBean.DataBean>();
        private Context context;

        public TuiHuoAdapter(Context context) {
            super();
            this.context = context;
        }

        public TuiHuoAdapter() {

        }

        public void setList(List<TuihuoBean.DataBean> list) {

            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (null == convertView) {
                convertView = inflater.inflate(R.layout.item_tuihuo, null);

                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final TuihuoBean.DataBean bean = info.get(position);
            viewHolder.cancelBn.setTag(position);
            viewHolder.rel_goods_show.setTag(position);
            String return_id=bean.getRefund_id();
            viewHolder.shopName.setText(bean.getStore_name());
            int state= Integer.parseInt(bean.getSeller_state());


             if(state==2){
                viewHolder.intentState.setText("待退款");
            }else if(state==3){
                viewHolder.intentState.setText("已完成");
            }else{
                 viewHolder.intentState.setText("退款中");
             }


            viewHolder.goodsIntroduce.setText(bean.getGoods_name());
            viewHolder.intentPrice.setText(bean.getRefund_amount());
            viewHolder.intentColor.setText(bean.getGoodParams());
            viewHolder.cancelBn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    nextAct(RefundProgressActivity.class);
                    String refund_type=bean.getRefund_type();
                    String refund_id=bean.getRefund_id();
                    Intent intent = new Intent();
                    intent.setClass(RefundSalesActivity.this, RefundProgressActivity.class);
                    intent.putExtra("Refund_id", refund_id);
                    intent.putExtra("refund_type",refund_type);
                    startActivity(intent);
                }
            });
            String image = bean.getGoods_image();
            Picasso.with(context)
                    .load(image)
                    .resize(200, 200)
                    .centerCrop()
                    .placeholder(R.drawable.launcher_icon)
                    .error(R.drawable.launcher_icon)
                    .into(viewHolder.goodsShow);


            viewHolder.rel_goods_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String goods_id=bean.getGoods_id();
                    Intent intent = new Intent();
                    intent.setClass(RefundSalesActivity.this, GoodsDetailActivity.class);
                    intent.putExtra("GoodId", goods_id);

                    startActivity(intent);
                }
            });

            return convertView;
        }


        class ViewHolder {
            @Bind(R.id.shop_name)
            TextView shopName;
            @Bind(R.id.intent_state)
            TextView intentState;
            @Bind(R.id.goods_show)
            ImageView goodsShow;
            @Bind(R.id.goods_introduce)
            TextView goodsIntroduce;
            @Bind(R.id.intent_price)
            TextView intentPrice;
            @Bind(R.id.intent_color)
            TextView intentColor;
            @Bind(R.id.intent_size)
            TextView intentSize;
            @Bind(R.id.cancel_bn)
            Button cancelBn;
            @Bind(R.id.rel_goods_show)
            RelativeLayout rel_goods_show;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    @OnClick(R.id.go_back)
    public void onClick() {
        finish();
    }
}
